//
//  KLGoodBuyDetailViewController.m
//  KunLun
//
//  Created by Mac on 2017/11/28.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLGoodBuyDetailViewController.h"
#import "NSString+STFormatter.h"
@interface KLGoodBuyDetailViewController ()
@property(nonatomic, strong) STLabel                     *titleLable;
@property(nonatomic, strong) STLabel                     *contactLable;
@property(nonatomic, strong) NSArray                     *imageArray;
@end

@implementation KLGoodBuyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"购买详情";
    [self sendDetailRequest];
    
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    
    self.titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, 0, UIScreenWidth, 44)
                                                     text:@"公司名字洒洒水多as大萨达啊"
                                                textColor:KL_redColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
  //  [headerView addSubview:self.titleLable];
    
//    NSString * desStr = @"1.当你收到货品时，请与快递员当面检查验收，如过后发现问题不予退还，如有问题，请现场拍照并第一时间与客服联系，一遍出现偏差影响您的购物心情，收到货后早早确认手术哦，积分早早返还哦！\n 2.偏远地区不包邮（新疆，西藏，青海，港澳台地区）运输成本高，发货到以上城市需要单独补充运费\n 请购买钱一定要吧联系方式写正确检查好，避免出现不必要麻烦，如若是因为地址电话不正确造成的订单退回，需要买家承担运费";
//    STLabel * desLable = [[STLabel alloc] initWithFrame:CGRectMake(10, 20, UIScreenWidth - 20, 10)
//                                                     text:desStr
//                                                textColor:KL_firstTextColor
//                                                     font:14
//                                              isSizetoFit:NO
//                                            textAlignment:NSTextAlignmentLeft];
//    [NSString lableAutoAdjustheightWithLabel:desLable];
//   [headerView addSubview:desLable];
    
    
    self.contactLable = [[STLabel alloc] initWithFrame:CGRectMake(10, 0 + 15, UIScreenWidth - 20, 10)
                                                   text:self.model.abstract
                                              textColor:KL_redColor
                                                   font:14
                                            isSizetoFit:NO
                                          textAlignment:NSTextAlignmentLeft];
     [NSString lableAutoAdjustheightWithLabel:self.contactLable];
    [headerView addSubview:_contactLable];
    
    CGFloat top = self.contactLable.bottom +10;
    for (int i =0; i < self.imageArray.count; i ++) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, top, UIScreenWidth - 40, 170)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[i]]];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        [headerView addSubview:imageView];
        top = imageView.bottom + 10;
        imageView.tag = 10000 + i;
        UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectedImageView:)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:ges];
    }
     headerView.height = top + 10 ;
    self.tableView.tableHeaderView = headerView;
    
}
- (void)sendDetailRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/goods/goodssynopsis"];
    [dic setObject:self.model.goodID forKey:@"goods_id"];

    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 if ([dataDic.allKeys containsObject:@"info"]) {
                                                     self.imageArray = dataDic[@"info"];
                                                 }
                                                 [self configSubView];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 
                                                 
                                             }];
}
#pragma mark -图片查看
- (void)onSelectedImageView:(UIGestureRecognizer*)ges{
    UIImageView * imageView = (id)ges.view;
    NSMutableArray * dataSouce = [NSMutableArray new];
    for (NSString * url in self.imageArray) {
        STUrlPhotoModel * model = [STUrlPhotoModel new];
        model.originImageUrl = url;
        model.thumbImageUrl = url;
        //        model.originImage = imageView.image;
        [dataSouce addObject:model];
    }
    NSInteger tag = imageView.tag - 10000;
    STPhotoUrlImageBrowerController *  vc = [[STPhotoUrlImageBrowerController alloc] initWithArray:dataSouce curentIndex:tag];
    vc.shouldHideBottomView = YES;
    [self presentViewController:vc animated:NO completion:nil];
}
@end
